package com.bmft.project02.util;


import java.io.IOException;
import java.io.InputStream;

/**
 * This class can help you to read text of the file.
 *
 * @author Jarvan
 * @version 1.0
 * @create 2020/9/23 18:23
 */
public class FileUtil {
    /**
     * Read file as String.
     *
     * @param resourceFileName file should put in resources folder.
     * @return string
     * @throws IOException
     */
    public static String readFileAsString(String resourceFileName) throws IOException {
        InputStream resourceAsStream = FileUtil.class.getClassLoader().getResourceAsStream(resourceFileName);
        byte[] bytes = new byte[1024];
        int length = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while ((length = resourceAsStream.read(bytes, 0, bytes.length)) > 0) {
            stringBuilder.append(new String(bytes, 0, length, "utf8"));
        }
        return stringBuilder.toString();
    }
}
